package com.oe.market.modules.ele.controller;


import com.google.gson.Gson;
import com.oe.market.modules.base.model.bean.Account;
import com.oe.market.modules.base.model.bean.RetResponse;
import com.oe.market.modules.base.model.dto.AccountDto;
import com.oe.market.modules.base.service.AccountService;
import com.oe.market.modules.common.validator.DtoValidator;
import com.oe.market.modules.ele.model.bean.Cookie;
import com.oe.market.modules.ele.model.bean.CookieUseCount;
import com.oe.market.modules.ele.model.dao.AccountDao;
import com.oe.market.modules.ele.model.dao.CookieDao;
import com.oe.market.modules.ele.model.dao.CookieUseCountDao;
import com.oe.market.modules.ele.model.dto.EleRequestDto;
import com.oe.market.modules.ele.service.EleRequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;
import java.util.List;

@RestController
@RequestMapping(value = "/v1/ele/request/")
@ResponseBody
public class EleRequestController {

    private static final Logger log = LoggerFactory.getLogger(EleRequestController.class);

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestBody String data, HttpServletRequest request) {
        Gson gson = new Gson();
        // 结构化消息类型
        EleRequestDto eleRequestDto = gson.fromJson(data, EleRequestDto.class);
        Account account = AccountService.check(request.getCookies());
        {
            if (account != null) {
                eleRequestDto.account = new AccountDto();
                eleRequestDto.account.user = account.user;
                eleRequestDto.account.currentSign = account.currentSign;
            }
            else {
                return RetResponse.failedByAccount().toString();
            }
        }

        try {
            DtoValidator.validate(eleRequestDto);
        }
        catch (ValidationException e) {
            log.error(e.getMessage());
            log.error("request: " + data);
            return e.getMessage();
        }

        String result = null;
        RetResponse retResponse = RetResponse.failed("未知错误",null);
        try {
            // 构建业务
            EleRequestService eleRequestService = new EleRequestService();
            result = eleRequestService.add(eleRequestDto, account.getAccountId());

        }
        catch (Exception e) {
            log.error(e.getMessage());
            log.error("request: " + data);
            retResponse.setMsg("非法操作");
        }


        if (null == result){
            retResponse = RetResponse.success();
            // 加分
            AccountDao.me.addMaxUse(account.getAccountId(), 5);
        }
        else {
            retResponse.setMsg(result);
        }
        return retResponse.toString();
    }


    @RequestMapping(value = "/query")
    public String query(HttpServletRequest request) {
        Account account = AccountService.check(request);
        if (null == account){
            return RetResponse.failedByAccount().toString();
        }
        List<CookieUseCount> requestList = CookieUseCountDao.me.searchValid(account.getAccountId());

        if (null == requestList) {
            return RetResponse.failed("该账户未贡献账号",requestList).toString();
        }

        return RetResponse.success("查询成功",requestList).toString();
    }

    @RequestMapping(value = "/query/full")
    public String queryFull(HttpServletRequest request) {
        Account account = AccountService.check(request);
        if (null == account){
            return RetResponse.failedByAccount().toString();
        }
        List<Cookie> requestList = CookieDao.me.findByAccountId(account.getAccountId());

        if (null == requestList) {
            return RetResponse.failed("该账户未贡献账号",requestList).toString();
        }
        return RetResponse.success("查询成功",requestList).toString();
    }


    @RequestMapping(value = "/query/single/{sns_hash}")
    public String querySingle(@PathVariable ("sns_hash") String snsHash, HttpServletRequest request) {

        Account account = AccountService.check(request);
        if (null == account){
            return RetResponse.failedByAccount().toString();
        }
        List<Cookie> cookies = CookieDao.me.findBySnsHash(snsHash);

        if (null == cookies || 0 == cookies.size()) {
            return RetResponse.failed("非法访问").toString();
        }
        return RetResponse.success("查询成功",cookies).toString();
    }

}
